/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ajz.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ajz.entity.*;
import com.jeesite.modules.ajz.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 历史记录Controller
 * @author cww
 * @version 2019-01-11
 */
@Controller
@RequestMapping(value = "${adminPath}/ajz/ajzRecordProcess")
public class AjzRecordProcessController extends BaseController {

	@Autowired
	private AjzRecordProcessService ajzRecordProcessService;

	@Autowired
	private AjzCompanyInfoService ajzCompanyInfoService;

	@Autowired
	private AjzHouseInfoService ajzHouseInfoService;

	@Autowired
	private AjzStoreInfoService ajzStoreInfoService;

	@Autowired
	private AjzRecordService ajzRecordService;

	@Autowired
	private AjzCheckService ajzCheckService;



	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AjzRecordProcess get(String id, boolean isNewRecord) {
		return ajzRecordProcessService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ajz:ajzRecordProcess:view")
	@RequestMapping(value = {"list", ""})
	public String list(AjzRecordProcess ajzRecordProcess,  HttpServletRequest request, HttpServletResponse response,Model model) {
		String base_id = request.getParameter("base_id");
		System.out.println(base_id);

		AjzCompanyInfo ajzCompanyInfo = ajzCompanyInfoService.get(base_id);
		ajzRecordProcess.setAjzCompanyInfo(ajzCompanyInfo);

		model.addAttribute("ajzRecordProcess", ajzRecordProcess);
		return "modules/ajz/ajzRecordProcessList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ajz:ajzRecordProcess:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AjzRecordProcess> listData(AjzRecordProcess ajzRecordProcess, HttpServletRequest request, HttpServletResponse response) {
		ajzRecordProcess.setPage(new Page<>(request, response));
		Page<AjzRecordProcess> page = ajzRecordProcessService.findPage(ajzRecordProcess); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ajz:ajzRecordProcess:view")
	@RequestMapping(value = "form")
	public String form(AjzRecordProcess ajzRecordProcess, Model model) {
		model.addAttribute("ajzRecordProcess", ajzRecordProcess);
		return "modules/ajz/ajzRecordProcessForm";
	}

	/**
	 * 保存历史记录
	 */
	@RequiresPermissions("ajz:ajzRecordProcess:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AjzRecordProcess ajzRecordProcess) {
		ajzRecordProcessService.save(ajzRecordProcess);
		return renderResult(Global.TRUE, text("保存历史记录成功！"));
	}
	
	/**
	 * 停用历史记录
	 */
	@RequiresPermissions("ajz:ajzRecordProcess:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(AjzRecordProcess ajzRecordProcess) {
		ajzRecordProcess.setStatus(AjzRecordProcess.STATUS_DISABLE);
		ajzRecordProcessService.updateStatus(ajzRecordProcess);
		return renderResult(Global.TRUE, text("停用历史记录成功"));
	}
	
	/**
	 * 启用历史记录
	 */
	@RequiresPermissions("ajz:ajzRecordProcess:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(AjzRecordProcess ajzRecordProcess) {
		ajzRecordProcess.setStatus(AjzRecordProcess.STATUS_NORMAL);
		ajzRecordProcessService.updateStatus(ajzRecordProcess);
		return renderResult(Global.TRUE, text("启用历史记录成功"));
	}
	
	/**
	 * 删除历史记录
	 */
	@RequiresPermissions("ajz:ajzRecordProcess:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AjzRecordProcess ajzRecordProcess) {
		ajzRecordProcessService.delete(ajzRecordProcess);
		return renderResult(Global.TRUE, text("删除历史记录成功！"));
	}


	@RequestMapping({"docScene"})
	public String docScene(HttpServletRequest request, AjzRecordProcess ajzRecordProcess, Model model)  {

		//获取企业信息和隐患信息
		AjzCompanyInfo ajzCompanyInfo = new AjzCompanyInfo();
		AjzStoreInfo ajzStoreInfo = new AjzStoreInfo();
		AjzRecord ajzRecord = new AjzRecord();
		AjzHouseInfo ajzHouseInfo = new AjzHouseInfo();
		String type = request.getParameter("type");
		String recUrl = "";
		if ("company".equals(type)) {
			ajzCompanyInfo = ajzCompanyInfoService.get(ajzRecordProcess.getBaseId());
			ajzRecord = ajzRecordService.findInfoById(ajzCompanyInfo.getId(),request.getParameter("tableType"));
			model.addAttribute("ajzCompanyInfo", ajzCompanyInfo);
			recUrl = "modules/ajz/mobile/doc_company";
		}
		if ("store".equals(type)) {
			ajzStoreInfo = ajzStoreInfoService.get(ajzRecordProcess.getBaseId());
			ajzRecord = ajzRecordService.findInfoById(ajzStoreInfo.getId(),request.getParameter("tableType"));
			model.addAttribute("ajzStoreInfo", ajzStoreInfo);
			recUrl = "modules/ajz/mobile/doc_store";
		}
		if ("house".equals(type)) {
			ajzHouseInfo = ajzHouseInfoService.get(ajzRecordProcess.getBaseId());
			ajzRecord = ajzRecordService.findInfoById(ajzHouseInfo.getId(),request.getParameter("tableType"));
			model.addAttribute("ajzHouseInfo", ajzHouseInfo);
			recUrl = "modules/ajz/mobile/doc_house";
		}


		List<AjzCheck> ajzChecks = new ArrayList<>();
		if(ajzRecord!=null){
			ajzChecks = ajzCheckService.findListByRecordProcessId(ajzRecordProcessService.findForNum(ajzRecord.getNumber()).getId());
		}

		//隐患问题描述
		StringBuffer nowProblem = new StringBuffer();
		int count = 0;
		List list = new ArrayList<>();
		for (AjzCheck check : ajzChecks) {
			count++;
			if("0".equals(check.getCheckStatus())){
				nowProblem.append(count+"、"+check.getProblem()+"(已整改)<br/>");
			} else if("2".equals(check.getCheckStatus())){
				nowProblem.append(count+"、"+check.getProblem()+"<span style=\"color: red\">*</span><br/>");
			} else if("3".equals(check.getCheckStatus())){
                nowProblem.append(count+"、"+check.getProblem()+"<span style=\"color: red\">*</span>(责令已整改)<br/>");
            } else if("4".equals(check.getCheckStatus())){
                nowProblem.append(count+"、"+check.getProblem()+"<span style=\"color: red\">*</span>(责令未整改)<br/>");
            } else{
				nowProblem.append(count+"、"+check.getProblem()+"<br/>");
			}
		}
		list.add(nowProblem);

		//隐患点照片拼接
		String checkUrls =  ajzRecordProcess.getScenePic() == null ? "" : ajzRecordProcess.getScenePic();
		String dangerUrls =  ajzRecordProcess.getDangerPic() == null ? checkUrls+"" : checkUrls+ajzRecordProcess.getDangerPic();
		String[] dgls = null;
		if (StringUtils.isNotBlank(dangerUrls)) {
			dgls = dangerUrls.split(";");
		}
		model.addAttribute("dgls", dgls);
		model.addAttribute("list", list); //返回复查信息


		model.addAttribute("record", ajzRecordProcess);
		return recUrl;
	}
	
}